//
//  StrategyDetailViewController.m
//  MyDIYHouse
//
//  Created by dlios on 15-6-30.
//  Copyright (c) 2015年 李平洋. All rights reserved.
//

#import "StrategyDetailViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "UIImageView+WebCache.h"
#import "StrategyDetailCollectionViewCell.h"
#import "StrategyDetailCollectionReusableView.h"
#import "StrategyDetailImageDetailViewController.h"


@interface StrategyDetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,StrategyDetailCollectionReusableViewDelegate>

//因为需要自适应,所以把flowLayout设成属性
@property (nonatomic,retain)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,retain)UICollectionView *collectionView;

@property (nonatomic,retain)StrategyDetailCollectionReusableView *myStrategyDetailCollectionReusableView;
//为了实现滚动,最底层加一个scrollview
@property (nonatomic,retain)UIScrollView *scrollview;



@end


@implementation StrategyDetailViewController

- (void)dealloc
{
    self.collectionView.delegate = nil;
    self.scrollview.delegate = nil;
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.title = self.myStrategy.title;
    //  self.tabBarController.tabBar.hidden = YES;
    
    self.scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH , HEIGHT-64)];
    self.scrollview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollview];
    [self.scrollview release];
    
    
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake((WIDTH-4)/3, (WIDTH-4)/3);
    self.flowLayout.minimumLineSpacing = 1;
    self.flowLayout.minimumInteritemSpacing = 1;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
    
    //头视图
    self.flowLayout.headerReferenceSize =CGSizeMake(0, 400);
    
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT*2) collectionViewLayout:self.flowLayout];
    // self.collectionView .tag = 1000;
    self.collectionView .backgroundColor = [UIColor whiteColor];
    self.collectionView .dataSource = self;
    self.collectionView .delegate =self;
    [self.scrollview addSubview:self.collectionView ];
    [self.collectionView  release];
    
    
    
    // 注册
    // 第一个参数:需要制定注册的类型
    // 第二个参数:重用标志
    [self.collectionView  registerClass:[StrategyDetailCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    // 头和尾的视图也需要注册
    // 第二个空,填UICollectionViewFlowLayout里第十三或十四行的东西
    // 第二个参数需要指定当前注册的是头还是尾,具体的字符串在UICollectionViewFlowLayout类的第十三十四行
    [self.collectionView  registerClass:[StrategyDetailCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    
    //    self.scrollview.contentSize = CGSizeMake(WIDTH, HEIGHT*3);
    
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
//{
////     self.flowLayout.headerReferenceSize =CGSizeMake(0, 120+self.myStrategyDetailCollectionReusableView.titleLabel.frame.size.height+self.myStrategyDetailCollectionReusableView.descriptionLabel.frame.size.height);
//    NSLog(@"aaa");
//    //NSLog(@"%f",(120+self.myStrategyDetailCollectionReusableView.titleLabel.frame.size.height+self.myStrategyDetailCollectionReusableView.descriptionLabel.frame.size.height));
//
//
//    return CGSizeMake(0, 120+self.myStrategyDetailCollectionReusableView.titleLabel.frame.size.height+self.myStrategyDetailCollectionReusableView.descriptionLabel.frame.size.height);
//}
//

- (void)changeSize
{
    self.flowLayout.headerReferenceSize =CGSizeMake(0, 120+self.myStrategyDetailCollectionReusableView.titleLabel.frame.size.height+self.myStrategyDetailCollectionReusableView.descriptionLabel.frame.size.height);
    
    NSInteger height = 0;
    if (self.myStrategy.photos.count%3 == 0) {
        height = ((WIDTH-4)/3+1)*(self.myStrategy.photos.count/3);
    }else
    {
        height = ((WIDTH-4)/3+1)*(self.myStrategy.photos.count/3+1);
    }
    
    self.scrollview.contentSize = CGSizeMake(WIDTH, 120+self.myStrategyDetailCollectionReusableView.titleLabel.frame.size.height+self.myStrategyDetailCollectionReusableView.descriptionLabel.frame.size.height+height);
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if(kind ==UICollectionElementKindSectionHeader )
    {
        self.myStrategyDetailCollectionReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        self.myStrategyDetailCollectionReusableView.delegate = self;
        self.myStrategyDetailCollectionReusableView.titleLabel.text = self.myStrategy.title;
        [self.myStrategyDetailCollectionReusableView.userPicImageView sd_setImageWithURL:[NSURL URLWithString:[[self.myStrategy.user objectForKey:@"userImage"] objectForKey:@"large"]]];
        self.myStrategyDetailCollectionReusableView.userNameLabel.text = [self.myStrategy.user objectForKey:@"userName"];
        self.myStrategyDetailCollectionReusableView.descriptionLabel.text = self.myStrategy.bDescription;
        
        
        //  self.flowLayout.headerReferenceSize =CGSizeMake(0, view.descriptionLabel.frame.origin.y+view.descriptionLabel.frame.size.height);
        
        // NSLog(@"11");
        
        self.myStrategyDetailCollectionReusableView.backgroundColor = [UIColor whiteColor];
        return self.myStrategyDetailCollectionReusableView;
    }else
        return nil;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.myStrategy.photos.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    StrategyDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    //使用了注册,就不需要判断是否为空,因为肯定不为空
    
    NSDictionary *photoDic =  self.myStrategy.photos[indexPath.row];
    
    NSString *picUrl = [NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w%@_h%@_m0.jpg",[photoDic objectForKey:@"id"],[photoDic objectForKey:@"width"],[photoDic objectForKey:@"height"]];
    
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:picUrl]];
    
    return cell;
    
}


#pragma mark 跳转下一页的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    StrategyDetailImageDetailViewController *imageDetailVC = [[StrategyDetailImageDetailViewController alloc] init];
    
    NSDictionary *photoDic =  self.myStrategy.photos[indexPath.row];
    
    imageDetailVC.urlId = [photoDic objectForKey:@"id"];
    // NSLog(@"%@",[photoDic objectForKey:@"id"]);
    [self.navigationController pushViewController:imageDetailVC animated:YES];
    [imageDetailVC release];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
